import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { visitlist , visitdelete } from '@/api/visit';
import {message} from 'antd'


export interface IndexModelState {
  name: string;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel:any = {
  namespace: 'dispatchlist2',

  state: {
    list:[]
  },

  effects: {
    *visitlist({ payload }:any, { call, put }:any):any {
      const visitgetlist:any = yield call (visitlist)

      console.log(visitgetlist);
      yield put({
        type:'setlist',
        payload:visitgetlist.data.data
      })

    },



                //删除
                *visitdelete({ payload }:any, { call, put }:any):any {
                  const result = yield call (visitdelete,payload)
                  console.log(result.data.data);

                     yield put({
                         type:'visitdel',
                         payload:payload
                     })

                     if(result.status === 200){
                       message.success('删除成功')
                     }
             },
  },
  reducers: {
    setlist(state:any, action:any) {
      console.log(action);
                 state.list = action.payload

      return {
        ...state,
        ...action.payload,
      };
    },



    //删除
    visitdel(state:any, action:any) {
      state.list.forEach((item:any,index:number) => {
               if(item.id === action.payload){
                 state.list.splice(index, 1)
               }
      });


     return {
       ...state,
       ...action.payload,
     };
   },


    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },

};

export default IndexModel;
